package cn.hunqing.web.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import cn.hunqing.web.dto.RetrievedFileDto;
import cn.hunqing.web.entity.UploadedFile;
import cn.hunqing.web.mapper.ApplicationConfigurationMapper;
import cn.hunqing.web.mapper.UploadedFileMapper;
import cn.hunqing.web.service.UploadedFileService;
import cn.hunqing.web.util.FileStoredPathUtils;

@Service
public class UploadedFileServiceImpl implements UploadedFileService {
	private static final Logger LOG = LoggerFactory
			.getLogger(UploadedFileServiceImpl.class.getName());
	@Autowired
	private ApplicationConfigurationMapper  applicationConfigurationMapper;
	@Autowired
	private UploadedFileMapper uploadedFileMapper;

	@Override
	public RetrievedFileDto getRetrievedFilePath(String fileId) {
		UploadedFile uploadedFile= uploadedFileMapper.get(fileId);
		if(uploadedFile != null){
			return RetrievedFileDto.buildFrom(uploadedFile);
		}
		return null;
	}

	@Override
	public String upload(MultipartFile multipartFile) {
		String storedPath = applicationConfigurationMapper.getStoredPath();
		if(StringUtils.isEmpty(storedPath)){
			throw new RuntimeException("没有配置存储路径");
		}
		File rootDir = new File(storedPath);
		if(!rootDir.exists()){
			throw new RuntimeException("存储路径配置不正确");
		}
		UploadedFile newUploadedFile = new UploadedFile();
		String uploadedFileId = UUID.randomUUID().toString();
		newUploadedFile.setId(uploadedFileId);
		newUploadedFile.setContentType(multipartFile.getContentType());
		newUploadedFile.setFileName(multipartFile.getOriginalFilename());
		newUploadedFile.setSize(multipartFile.getSize());
		String absolutePath = FileStoredPathUtils.generatePath(rootDir, uploadedFileId,
				multipartFile.getOriginalFilename());

		newUploadedFile.setAbsolutePath(absolutePath);
		try {
			multipartFile.transferTo(new File(absolutePath));
		} catch (IllegalStateException e) {
			LOG.error("store file exception:", e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		uploadedFileMapper.insert(newUploadedFile);
		
		return uploadedFileId;
	}
	
	@Override
	public String upload(byte[] fileByte, String originalFileName) {
		String storedPath = applicationConfigurationMapper.getStoredPath();
		if(StringUtils.isEmpty(storedPath)){
			throw new RuntimeException("没有配置存储路径");
		}
		File rootDir = new File(storedPath);
		if(!rootDir.exists()){
			throw new RuntimeException("存储路径配置不正确");
		}
		UploadedFile newUploadedFile = new UploadedFile();
		String uploadedFileId = UUID.randomUUID().toString();
		newUploadedFile.setId(uploadedFileId);
		newUploadedFile.setContentType("image/jpeg");
		newUploadedFile.setFileName(originalFileName);
		newUploadedFile.setSize(fileByte.length);
		String absolutePath = FileStoredPathUtils.generatePath(rootDir, uploadedFileId,
				originalFileName);

		newUploadedFile.setAbsolutePath(absolutePath);
		try {
			//multipartFile.transferTo(new File(absolutePath));
			OutputStream out = new FileOutputStream(absolutePath);
			out.write(fileByte);
			out.flush();
			out.close();
		} catch (IllegalStateException e) {
			LOG.error("store file exception:", e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		uploadedFileMapper.insert(newUploadedFile);
		
		return uploadedFileId;
	}

}
